package io.terminus.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.terminus.spzx.manager.service.ProductService;
import io.terminus.spzx.model.dto.product.ProductDto;
import io.terminus.spzx.model.dto.product.SkuSaleDto;
import io.terminus.spzx.model.entity.product.Product;
import io.terminus.spzx.model.vo.common.Result;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 商品管理Controller
 * @Date 2024-10-11
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "商品管理接口")
@RequestMapping(value = "/admin/product/product")
public class ProductController {

    private final ProductService productService;

    /**
     * 分页查询商品列表
     *
     * @param page       当前页
     * @param limit      每页记录数
     * @param productDto 查询条件
     * @return 分页查询结果
     */
    @GetMapping(value = "/{page}/{limit}")
    public Result<PageInfo<Product>> findByPage(@PathVariable Integer page, @PathVariable Integer limit, ProductDto productDto) {
        PageInfo<Product> pageInfo = productService.findByPage(page, limit, productDto);

        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询商品详情
     *
     * @param id 商品ID
     * @return 商品详情
     */
    @GetMapping("/getById/{id}")
    public Result<Product> getById(@PathVariable Long id) {
        Product product = productService.getById(id);

        return Result.build(product, ResultCodeEnum.SUCCESS);
    }

    /**
     * 保存商品
     *
     * @param product 商品
     * @return 保存结果
     */
    @PostMapping("/save")
    public Result<Void> save(@RequestBody Product product) {
        productService.save(product);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改商品
     *
     * @param product 商品
     * @return 修改结果
     */
    @PutMapping("/updateById")
    public Result<Void> updateById(@Parameter(name = "product", description = "请求参数实体类", required = true) @RequestBody Product product) {
        productService.updateById(product);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 审核商品
     *
     * @param id          商品ID
     * @param auditStatus 审核状态
     * @return 审核结果
     */
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public Result<Void> updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
        productService.updateAuditStatus(id, auditStatus);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 上下架商品
     *
     * @param id     商品ID
     * @param status 上下架状态
     * @return 上下架结果
     */
    @GetMapping("/updateStatus/{id}/{status}")
    public Result<Void> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        productService.updateStatus(id, status);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除商品
     *
     * @param id 商品ID
     * @return 删除结果
     */
    @DeleteMapping("/deleteById/{id}")
    public Result<Void> deleteById(@Parameter(name = "id", description = "商品id", required = true) @PathVariable Long id) {
        productService.deleteById(id);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("updateSkuSaleNum")
    @Operation(summary = "更新商品sku销量")
    public Boolean updateSkuSaleNum(@RequestBody List<SkuSaleDto> skuSaleDtoList) {
        return productService.updateSkuSaleNum(skuSaleDtoList);
    }
}
